<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-09-26 16:56:08
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\Material;
use App\Models\MaterialCategory;
use App\Models\ProDyecraft;
use App\Models\ProDyecraftGroup;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

/**
 * @property ProDyecraftGroup $group
 */
class ProDyecraftRepository extends Repositories
    {
    public $relation_models = ["updator", 'category'];

    public function boot(ProDyecraft $model, ProDyecraftGroup $group)
        {
        $this->model = $model;
        $this->group = $group;
        }

    public function fuzzySearch($value)
        {
        $value = trim($value);
        $this->where(function ($query) use ($value)
            {
            $query->orWhere("name", "like", "%{$value}%");
            $query->orWhere("code", "like", "%{$value}%");
            });
        }
    public function condition($request = null, $withauth = true)
        {
        if ($request == null) {
            $request = request();
            }
        if ($request->has("filter")) {
            $filters = $request->filter;
            foreach ($filters ?? [] as $keys => $filter) {
                foreach ($filter ?? [] as $key => $value) {
                    if ($key == 'materialid') {
                        $this->where("code", "like", $this->conditionCategory($value, "like") . "%");
                        unset($filters[$keys]['materialid']);
                        }
                    if ($key == 'loomid') {
                        $this->where("loomIds", "like", "%{$value}%");
                        unset($filters[$keys]['loomid']);
                        }
                    if ($key == 'rawids') {
                        array_multisort($value);
                        $value = implode(",", $value);
                        $this->where("rawIds", "like", "%{$value}%");
                        unset($filters[$keys]['rawids']);
                        }
                    if ($key == 'parent_code') {
                        $codes = $this->group->whereDyescraft_code($value)->pluck('endscraft_code');
                        $this->whereIn("code", $codes);
                        unset($filters[$keys]['parent_code']);
                        }
                    }
                }
            $request->offsetSet("filter", $filters);
            }
        return parent::condition($request, $withauth);
        }
    public function conditionCategory($material_id, $comp_ = "", $ColorShade = 0, $dyebath_ = 0)
        {
        $category = $this->getChildenCategory(['225', '247', '256', '226', '227'], $material_id);
        $code     = [];
        if ($category == 225) {
            $code[0] = 'B';
            }
        else if ($category == 247) {
            $code[0] = 'Z';
            }
        else if ($category == 256) {
            $code[0] = 'Z';
            }
        else if ($category == 226) {
            $code[0] = 'S';
            }
        else if ($category == 227) {
            $code[0] = 'F';
            }
        else {
            $code[0] = "_";
            }

        if ($comp_ == '高捻') {
            $code[1] = "G";
            }
        else if ($comp_ == '抗起球') {
            $code[1] = "K";
            }
        else if ($comp_ == '新纤棉' || $comp_ == '新纤/棉') {
            $code[1] = "X";
            }
        else if ($comp_ == '正常') {
            $code[1] = "Z";
            }
        else if ($comp_ == '天丝') {
            $code[1] = "T";
            }
        else if ($comp_ == '混纺纱') {
            $code[1] = "H";
            }
        else {
            $code[1] = "_";
            }
        $code[2] = is_numeric($ColorShade) && $ColorShade > 0 ? $ColorShade : "_";
        $code[3] = is_numeric($dyebath_) && $dyebath_ > 0 ? $dyebath_ : "_";
        return implode("", $code);
        }
    private function getChildenCategory($categoryids, $material_)
        {
        $allcategory         = MaterialCategory::get();
        $material_categoryid = Material::whereId($material_)->value("category_id");
        foreach ($categoryids as $vcid) {
            $resids = findAllChildIdArray($allcategory, [$vcid]);
            if (in_array($material_categoryid, $resids)) {
                return $vcid;
                }
            }

        return false;
        }
    }